<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 按订单开票 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<!-- 内容区 -->
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 顶部导航 -->
		<view class="container-header flex flex-between-center">
			<view class="header-title">
				<view class="text active">代驾订单</view>
			</view>
			<view class="header-filter flex align-items-center" @click="resetFilter()" v-if="filterDate && filterDate.start_time && filterDate.end_time">
				<view class="filter-icon" :style="{'background-image': 'url('+ iconFilter2 +')'}" v-if="iconFilter2"></view>
				<text class="filter-text" :class="{active: filterDate && filterDate.start_time && filterDate.end_time}">筛选</text>
			</view>
			<view class="header-filter" v-else>
				<uni-datetime-picker type="daterange" @change="changeFilter">
					<view class="flex align-items-center">
						<view class="filter-icon" :style="{'background-image': 'url('+ iconFilter1 +')'}" v-if="iconFilter1"></view>
						<text class="filter-text">筛选</text>
					</view>
				</uni-datetime-picker>
			</view>
		</view>
		<!-- 订单列表 -->
		<view class="container-main">
			<order-invoice :show-data="orderList" :loading="orderLoading" @changeSelect="changeSelect" v-if="orderList.length"></order-invoice>
			<empty top="36%" title="暂无相关内容~" v-else></empty>
		</view>
		<!-- 底部按钮 -->
		<view class="container-footer">
			<view class="footer-statistics">
				<text class="text">{{selectedList.length || 0}}</text>个行程，共<text class="text">{{totalPrice || 0}}</text>元
			</view>
			<view class="footer-btn" @click="toApply()">下一步</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import svgData from "@/common/svg.js"
	import orderInvoice from "@/pages/passenger/component/order/invoice.vue"
	export default {
		components: {
			orderInvoice,
		},
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 订单列表
				orderList: [],
				// 筛选日期
				filterDate: null,
				// 筛选图标-未选
				iconFilter1: "",
				// 分类查询参数
				page: 1,
				limit: 10,
				hasMore: false,
				// 订单列表是否加载
				orderLoading: false,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
				shareTitle: state => state.app.shareTitle,
				shareImage: state => state.app.shareImage,
				iconFilter2: state => {
					return svgData.svgToUrl("filter", state.app.themeColor)
				},
			}),
			totalPrice() {
				var result = this.orderList.filter(item => item.selected).reduce((sum, item) => sum + parseFloat(item.actual_amount), 0)
				return result ? parseFloat(result).toFixed(2) : 0;
			},
			selectedList() {
				return this.orderList.filter(item => item.selected);
			},
		},
		onLoad() {
			this.iconFilter1 = svgData.svgToUrl("filter", "#242629")
			uni.showLoading({
				title: "加载中"
			})
			this.getOrderList(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onShow() {
			if (this.loadEnd) {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
				this.page = 1
				this.orderLoading = true
				this.getOrderList(() => {
					this.orderLoading = false
				})
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.orderLoading = true
			this.getOrderList(() => {
				this.orderLoading = false
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.getOrderList()
			}
		},
		methods: {
			// 获取订单列表
			getOrderList(fn) {
				var data = {
					page: this.page,
					limit: this.limit,
				}
				if (this.filterDate && this.filterDate.start_time && this.filterDate.end_time) {
					data.start_time = this.filterDate.start_time
					data.end_time = this.filterDate.end_time
				}
				this.$util.request("passenger.invoice.order", data).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						let list = res.data.data
						this.hasMore = this.page < res.data.total / this.limit ? true : false
						this.orderList = this.page == 1 ? list : [...this.orderList, ...list];
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取订单列表 ', error)
				})
			},
			// 更改筛选日期
			changeFilter(date) {
				this.filterDate = {
					start_time: date[0],
					end_time: date[1],
				}
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				this.orderLoading = true
				this.getOrderList(() => {
					this.orderLoading = false
					uni.hideLoading()
				})
			},
			// 重置筛选日期
			resetFilter() {
				this.filterDate = null
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				this.orderLoading = true
				this.getOrderList(() => {
					this.orderLoading = false
					uni.hideLoading()
				})
			},
			// 更改选择
			changeSelect(index) {
				var data = this.orderList[index]
				data.selected = !data.selected
				this.$set(this.orderList, index, data)
			},
			// 跳转开票
			toApply() {
				const idList = this.selectedList.map(item => item.id) || []
				this.$util.toPage({
					mode: 1,
					path: `/pages/passenger/mine/invoice/apply?id=${idList.join()}&price=${this.totalPrice}`,
					isToken: true,
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding-bottom: 184rpx;

		.container-header {
			background: #ffffff;
			position: sticky;
			top: 0;
			z-index: 99;
			padding: 0 32rpx;

			.header-title {
				padding: 0 32rpx;
				display: inline-flex;
				justify-content: center;

				.text {
					padding: 28rpx 0 20rpx;
					color: var(--theme-color);
					font-size: 28rpx;
					line-height: 40rpx;
					text-align: center;
					border-bottom: 8rpx solid var(--theme-color);
				}
			}

			.header-filter {
				.filter-icon {
					width: 40rpx;
					height: 40rpx;
					background-size: 40rpx;
				}

				.filter-text {
					margin-left: 8rpx;
					color: #242629;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;

					&.active {
						color: var(--theme-color);
					}
				}
			}
		}

		.container-main {
			padding: 32rpx;
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 98;
			padding: 20rpx 32rpx 16rpx;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;

			.footer-statistics {
				color: #242629;
				font-size: 28rpx;
				font-weight: 600;
				line-height: 40rpx;

				.text {
					color: var(--theme-color);
				}
			}

			.footer-btn {
				margin-top: 28rpx;
				color: #ffffff;
				font-size: 28rpx;
				line-height: 40rpx;
				padding: 20rpx 24rpx;
				border-radius: 40rpx;
				text-align: center;
				background: var(--theme-color);
			}
		}
	}
</style>